using System;
using System.Collections.Generic;
using System.Linq;
namespace WebClient.Controllers
{
	#region using
	using System.Web.Mvc;
	using WebClient.Models.ViewModels;
	using WebClient.Models;
	using WebClient.Models.Repositories;
	using WebClient.Helpers;
	using System.Web.Security;
    using WebClient.CF_DAL_WS;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectOrderDetail()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL
			};
			return View(CreateViewModel(data));
		}


		public ActionResult SelectOrderDetailByIDOrder(int idorder, int idrestaurant)
		{
			var data = new DataTransferViewModel
			{
				IDOrder = idorder,
                IDRestaurant = idrestaurant,
				EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL,
			};
            return View("SelectOrders", CreateViewModel(data));
		}

        public ActionResult AdminSelectOrderDetailByIDOrder(int idorder, int page = 1)
        {
            var data = new DataTransferViewModel
            {
                IDOrder = idorder,
                EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL,
                CurrentPage = page
            };
            return View("AdminSelectOrders", CreateViewModel(data));
        }

		public ActionResult SelectOrderDetailByIDOrderPaging(int idorder)
		{
			var data = new DataTransferViewModel
			{
				IDOrder = idorder,
				EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL,
			};
			return PartialView("Templates/TH_ListOrderDetail", CreateViewModel(data));
		}

		public ActionResult SelectOrderDetailByIDFood(int idfood)
		{
			var data = new DataTransferViewModel
			{
				IDFood = idfood,
				EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL,
			};
			return View("SelectOrderDetail", CreateViewModel(data));
		}

		public ActionResult SelectOrderDetailByIDFoodPaging(int idfood)
		{
			var data = new DataTransferViewModel
			{
				IDFood = idfood,
				EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL,
			};
			return PartialView("Templates/TH_ListOrderDetail", CreateViewModel(data));
		}


		public ActionResult DeleteOrderDetail(int id, int page = 1)
		{
			var result = _repOrderDetail.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL,
				CurrentPage = page
			};
			return View("SelectOrderDetail", CreateViewModel(data));
		}

		public ActionResult EditOrderDetail(int page, int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL,
				CurrentPage = page,
				IntID = id
			};
			return PartialView("Templates/TH_EditOrderDetail", CreateViewModel(data));
		}

		public ActionResult CancelEditingOrderDetail(int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListOrderDetail", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateOrderDetail(FormCollection forms)
		{
			string id = forms.GetValues("OrderDetail_ID")[0];
			string idorder = forms.GetValues("OrderDetail_IDOrder")[0];
			string idfood = forms.GetValues("OrderDetail_IDFood")[0];
			string quantity = forms.GetValues("OrderDetail_Quantity")[0];
			string price = forms.GetValues("OrderDetail_Price")[0];
			var orderdetail = _repOrderDetail.SelectByID(int.Parse(id));
			orderdetail.IDOrder = int.Parse(idorder.Replace(".", "").Replace(",", ""));
			orderdetail.IDFood = int.Parse(idfood.Replace(".", "").Replace(",", ""));
			orderdetail.Quantity = int.Parse(quantity.Replace(".", "").Replace(",", ""));
            if (price != "")
            {
                orderdetail.Price = float.Parse(price);
            }
			var result = _repOrderDetail.Update(orderdetail);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL,
			};
			return View("SelectOrderDetail", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertOrderDetail(FormCollection forms)
		{
			string idorder = forms.GetValues("OrderDetail_IDOrder")[0];
			string idfood = forms.GetValues("OrderDetail_IDFood")[0];
			string quantity = forms.GetValues("OrderDetail_Quantity")[0];
			string price = forms.GetValues("OrderDetail_Price")[0];
			var newItem = new OrderDetailDTO
			{
				IDOrder = int.Parse(idorder),
				IDFood = int.Parse(idfood),
				Quantity = int.Parse(quantity)
			};
            if (price != "")
            {
                newItem.Price = float.Parse(price);
            }
			var result = _repOrderDetail.Insert(newItem);
			var data = new DataTransferViewModel
			{
				IDOrder = int.Parse(idorder),
				IDFood = int.Parse(idfood),
				EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectOrderDetail", CreateViewModel(data));
		}


	}
}